﻿using System;
using System.Web.Mvc;
using DNT.RavenQA.Web.Infrastructure.Registry;
using StructureMap;

namespace DNT.RavenQA.Web.Infrastructure.IoC
{
    public class StructureMapBootstrapper
    {
        private static readonly Object Sync = new Object();
        private static Boolean _initialized;

        public static void Initialize()
        {
            if (_initialized)
                return;

            lock (Sync)
            {
                if (_initialized)
                    return;

                DependencyResolver.SetResolver(new StructureMapDependencyResolver(BuildContainer()));
                _initialized = true;
            }
        }

        private StructureMapBootstrapper() { }

        private static IContainer BuildContainer()
        {
            ObjectFactory.Initialize(x => x.AddRegistry<ApplicationRegistry>());

#if DEBUG
            System.Diagnostics.Debug.Write(ObjectFactory.WhatDoIHave());
#endif

            return ObjectFactory.Container;
        }
    }
}